﻿using Autofac;
using Common;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DBContext;
using EV.EventBus;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using ZoneLine.Common;
using ZoneLine.Models;
using ZoneLine.Models.PrintModeConfig;
using ZoneLine.Models.StationConfig;
using ZoneLine.Printers;
using ZoneLine.Stations;

namespace ZoneLine.ViewModels
{
    public class HOpMainViewModel : ObservableObject
    {
        private IModelDAL _dal;
        public HOpMainViewModel(IModelDAL dal, HOP010Station h010Station, HOP040Station h040Station)
        {
            _dal = dal;

            InitStationInfo();

            var stationHOp010Config = _dal.GetModel<HOP010StationConfig>(p => true);
            if (stationHOp010Config == null)
            {
                stationHOp010Config = new HOP010StationConfig();
                _dal.AddModel(stationHOp010Config);
            }
            H010Station = h010Station;
            H010Station.Init(stationHOp010Config.Ip, 102, stationHOp010Config.ReadStartAddr, stationHOp010Config.EndUploadCodeAddr, stationHOp010Config.EndUploadDataAddr, stationHOp010Config.AllowWorkAddr);

            var stationHOp040Config = _dal.GetModel<HOP040StationConfig>(p => true);
            if (stationHOp040Config == null)
            {
                stationHOp040Config = new HOP040StationConfig();
                _dal.AddModel(stationHOp010Config);
            }
            H040Station = h040Station;
            H040Station.Init(stationHOp040Config.Ip, 102, stationHOp040Config.ReadStartAddr, stationHOp040Config.EndUploadCodeAddr, stationHOp040Config.EndUploadDataAddr, stationHOp040Config.AllowWorkAddr, stationHOp040Config.PrinterName);


            try
            {
                ReloadPrintConfig();
            }
            catch (Exception ex)
            {
                //todo 加载打印配置失败
            }

            Task.Run(() =>
            {
                H010Station.Start();
            });

            Task.Run(() =>
            {
                H040Station.Start();
            });
        }

        private void InitStationInfo()
        {
            var config = HStationEnableConfig.GetPara();
            if (config.HOP010StationEnable == 1)
            {
                Cache.WorkStationValidationList.Add(nameof(HOP010Station));
            }
            if (config.HOP040StationEnable == 1)
            {
                Cache.WorkStationValidationList.Add(nameof(HOP040Station));
            }
        }

        private HOP010Station _h010Station;

        public HOP010Station H010Station
        {
            get { return _h010Station; }
            set => SetProperty(ref _h010Station, value);
        }


        private HOP040Station _h040Station;

        public HOP040Station H040Station
        {
            get { return _h040Station; }
            set => SetProperty(ref _h040Station, value);
        }
        #region 打印配置

        private bool _enterAuth;

        public bool EnterAuth
        {
            get { return _enterAuth; }
            set => SetProperty(ref _enterAuth, value);
        }



        private RelayCommand _savePrintConfigCmd;

        public RelayCommand SaveConfigCmd
        {
            get { return _savePrintConfigCmd ?? (_savePrintConfigCmd = new RelayCommand(SavePrintConfig)); }
        }

        private void SavePrintConfig()
        {
            var config = ContainerServer.Container.Resolve<PrintConfigTotal>();
            try
            {
                _dal.UpdateModelSuper(p => true, config.LxqcConfig);
            }
            catch (Exception ex)
            {
                MessageBox.Show("理想汽车打印配置保存失败" + ex.Message);
            }

            try
            {
                _dal.UpdateModelSuper(p => true, config.Sqqc_Config);
            }
            catch (Exception ex)
            {
                MessageBox.Show("上汽汽车打印配置保存失败" + ex.Message);
            }

            try
            {
                _dal.UpdateModelSuper(p => true, config.BqqcLHConfig);
            }
            catch (Exception ex)
            {
                MessageBox.Show("北汽汽车螺簧打印配置保存失败" + ex.Message);
            }

            try
            {
                _dal.UpdateModelSuper(p => true, config.BqqcKhConfig);
            }
            catch (Exception ex)
            {
                MessageBox.Show("北汽汽车空簧打印配置保存失败" + ex.Message);
            }

            try
            {
                _dal.UpdateModelSuper(p => true, config.WlqcConfig);
            }
            catch (Exception ex)
            {
                MessageBox.Show("蔚来汽车打印配置保存失败" + ex.Message);
            }

            try
            {
                _dal.UpdateModelSuper(p => true, config.PackageConfigModel);
            }
            catch (Exception ex)
            {
                MessageBox.Show("打包打印配置保存失败" + ex.Message);
            }
        }

        private RelayCommand _reloadPrintConfigCmd;

        public RelayCommand ReloadConfigCmd
        {
            get { return _reloadPrintConfigCmd ?? (_reloadPrintConfigCmd = new RelayCommand(ReloadPrintConfig)); }
        }

        private void ReloadPrintConfig()
        {
            var config = ContainerServer.Container.Resolve<PrintConfigTotal>();

            var lxqcConfig = _dal.GetModel<LXQC_Config>(p => true);
            if (lxqcConfig == null)
            {
                _dal.AddModel(new LXQC_Config());
                lxqcConfig = new LXQC_Config();
            }
            config.LxqcConfig = lxqcConfig;


            var sqqcConfig = _dal.GetModel<SQQC_Config>(p => true);
            if (sqqcConfig == null)
            {
                _dal.AddModel(new SQQC_Config());
                sqqcConfig = new SQQC_Config();
            }
            config.Sqqc_Config = sqqcConfig;


            var bqqcLHConfig = _dal.GetModel<BQQC_LH_Config>(p => true);
            if (bqqcLHConfig == null)
            {
                _dal.AddModel(new BQQC_LH_Config());
                bqqcLHConfig = new BQQC_LH_Config();
            }
            config.BqqcLHConfig = bqqcLHConfig;

            var bqqcKHConfig = _dal.GetModel<BQQC_KH_Config>(p => true);
            if (bqqcKHConfig == null)
            {
                _dal.AddModel(new BQQC_KH_Config());
                bqqcKHConfig = new BQQC_KH_Config();
            }
            config.BqqcKhConfig = bqqcKHConfig;



            var wlConfig = _dal.GetModel<WLQCConfig>(p => true);
            if (wlConfig == null)
            {
                _dal.AddModel(new WLQCConfig());
                wlConfig = new WLQCConfig();
            }
            config.WlqcConfig = wlConfig;

        }


        private RelayCommand<string> _selectTemplateCmd;

        public RelayCommand<string> SelectTemplateCmd
        {
            get { return _selectTemplateCmd ?? (_selectTemplateCmd = new RelayCommand<string>(SelectTemplate)); }
        }

        private void SelectTemplate(string info)
        {
            FileDialog dia = new OpenFileDialog();
            var rlt = dia.ShowDialog();
            string path = string.Empty;
            if (rlt.HasValue && rlt.Value)
            {
                path = dia.FileName;
            }
            else
            {
                return;
            }
            var config = ContainerServer.Container.Resolve<PrintConfigTotal>();
            switch (info)
            {
                case "LX":
                    config.LxqcConfig.TemplatePath = path;
                    break;
                case "SQ":
                    config.Sqqc_Config.TemplatePath = path;
                    break;
                case "BQLH":
                    config.BqqcLHConfig.TemplatePath = path;
                    break;
                case "BQKH":
                    config.BqqcKhConfig.TemplatePath = path;
                    break;
                case "WL":
                    config.WlqcConfig.TemplatePath = path;
                    break;
                default:
                    break;
            }
        }

        private int _printIndex = 1000;

        public int PrintIndex
        {
            get { return _printIndex; }
            set => SetProperty(ref _printIndex, value);
        }


        private RelayCommand<string> _printTestCmd;

        public RelayCommand<string> PrintTestCmd
        {
            get { return _printTestCmd ?? (_printTestCmd = new RelayCommand<string>(PrintTest)); }
        }

        private void PrintTest(string s)
        {
            try
            {
                var config = _dal.GetModel<HOP040StationConfig>(p => true);
                var printConfig = ContainerServer.Container.Resolve<PrintConfigTotal>();
                switch (s)
                {
                    case "LX":
                        var printerLx = new LXQCPrinter();
                        printerLx.PrintLabel(printConfig.LxqcConfig.TemplatePath, config.PrinterName, PrintIndex);
                        break;
                    case "SQ":
                        var printerSq = new SQQCPrinter();
                        printerSq.PrintLabel(printConfig.Sqqc_Config.TemplatePath, config.PrinterName, PrintIndex);
                        break;
                    case "BQLH":
                        var printerBqLh = new BQLHPrinter();
                        printerBqLh.PrintLabel(printConfig.BqqcLHConfig.TemplatePath, config.PrinterName, PrintIndex);
                        break;
                    case "BQKH":
                        var printerBqKh = new BQKHPrinter();
                        printerBqKh.PrintLabel(printConfig.BqqcKhConfig.TemplatePath, config.PrinterName, PrintIndex);
                        break;
                    case "WL":
                        var printerWl = new WLQCPrinter();
                        printerWl.PrintLabel(printConfig.WlqcConfig.TemplatePath, config.PrinterName, PrintIndex);
                        break;

                    case "LX_Show":
                        Task.Run(() =>
                        {
                            var printerLx_show = new LXQCPrinter();
                            printerLx_show.PrintLabelShow(printConfig.LxqcConfig.TemplatePath, config.PrinterName, PrintIndex);
                        });

                        break;
                    case "SQ_Show":
                        Task.Run(() =>
                        {
                            var printerSq_show = new SQQCPrinter();
                            printerSq_show.PrintLabelShow(printConfig.Sqqc_Config.TemplatePath, config.PrinterName, PrintIndex);
                        });
                        break;
                    case "BQLH_Show":
                        Task.Run(() =>
                        {
                            var printerBqLh_show = new BQLHPrinter();
                            printerBqLh_show.PrintLabelShow(printConfig.BqqcLHConfig.TemplatePath, config.PrinterName, PrintIndex);
                        });
                        break;
                    case "BQKH_Show":
                        var printerBqKh_show = new BQKHPrinter();
                        printerBqKh_show.PrintLabelShow(printConfig.BqqcKhConfig.TemplatePath, config.PrinterName, PrintIndex);
                        break;
                    case "WL_Show":
                        var printerWl_show = new WLQCPrinter();
                        printerWl_show.PrintLabelShow(printConfig.WlqcConfig.TemplatePath, config.PrinterName, PrintIndex);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        #endregion

        #region 装箱

        private PackageBoxViewModel _packageBoxView;

        public PackageBoxViewModel PackageBoxView
        {
            get { return _packageBoxView; }
            set => SetProperty(ref _packageBoxView, value);
        }


        #endregion

        #region 重打50工位标签

        private RelayCommand<string> _rePrintCmd;

        public RelayCommand<string> RePrintCmd
        {
            get { return _rePrintCmd ?? (_rePrintCmd = new RelayCommand<string>(RePrint)); }
        }

        private void RePrint(string obj)
        {
            MessageCenter.PublishAsync(obj);
        }


        #endregion

    }
}
